package com.dyypdadmin.controller;

import com.dyypdadmin.pojo.ServiceDetails;
import com.dyypdadmin.result.Result;
import com.dyypdadmin.service.ServiceDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/servicedetails")
public class ServiceDetailsController {

    @Autowired
    private ServiceDetailsService serviceDetailsService;

    @PostMapping("/add")
    public Result addServiceDetails(@RequestBody ServiceDetails serviceDetails) {
        Integer serviceTypeId = serviceDetails.getService_type_id();
        String serviceName = serviceDetails.getService_name();
        String servicePrice = serviceDetails.getService_price();
        String serviceDescription = serviceDetails.getService_description();
        int result = serviceDetailsService.addServiceDetails(serviceTypeId, serviceName, servicePrice, serviceDescription);
        if (result > 0) {
            return new Result(true, "服务详情添加成功。");
        } else {
            return new Result(false, "服务详情添加失败。");
        }
    }


    @PutMapping("/update")
    public Result updateServiceDetails(@RequestBody ServiceDetails serviceDetails) {
        int result = serviceDetailsService.updateServiceDetails(serviceDetails);
        if (result > 0) {
            return new Result(true, "服务详情更新成功。");
        } else {
            return new Result(false, "服务详情更新失败。");
        }
    }

    @GetMapping("/delete/{id}")
    public Result deleteServiceDetails(@PathVariable("id") int id) {
        int result = serviceDetailsService.deleteServiceDetailsById(id);
        if (result > 0) {
            return new Result(true, "服务详情删除成功。");
        } else {
            return new Result(false, "服务详情删除失败。");
        }
    }

    @GetMapping("/{service_detail_id}")
    public Result getServiceDetailsById(@PathVariable("service_detail_id") int service_detail_id) {
        ServiceDetails serviceDetails = serviceDetailsService.getServiceDetailsById(service_detail_id);
        if (serviceDetails != null) {
            return new Result(true, serviceDetails);
        } else {
            return new Result(false, "未找到服务详情。");
        }
    }

    @GetMapping("/all")
    public Result getAllServiceDetails() {
        List<ServiceDetails> serviceDetailsList = serviceDetailsService.getAll();
        if (!serviceDetailsList.isEmpty()) {
            return new Result(true, serviceDetailsList);
        } else {
            return new Result(false, "未找到服务详情。");
        }
    }

    @GetMapping("/getByType/{service_type_id}")
    public Result getByType(@PathVariable("service_type_id") int service_type_id) {
        List<ServiceDetails> serviceDetailsList = serviceDetailsService.getByType(service_type_id);
        if (!serviceDetailsList.isEmpty()) {
            return new Result(true, serviceDetailsList);
        } else {
            return new Result(false, "未找到服务详情。");
        }
    }
    /**
     * @description:修改服务的上下架情况
     * @author:邓盈盈
     * @date: 2024/3/8 下午 01:41
     * @param:
     * @return:
     **/
    @GetMapping("/changeStatus/{service_detail_id}")
    public Result changeStatus(@PathVariable("service_detail_id")Integer service_detail_id){
        //获取服务详情
        ServiceDetails serviceDetail = serviceDetailsService.getServiceDetailsById(service_detail_id);
        int result;
        if(serviceDetail.getIs_del()==0){
            //未下架状态
             result = serviceDetailsService.changeStatus(service_detail_id,0,1);

        }else{
           result = serviceDetailsService.changeStatus(service_detail_id,1,0);
        }
        if(result>0){
            return new Result(true,"success");
        }else{
            return new Result(false,"fail");
        }

    }
    /**
     * @description:根据服务名称获取数据
     * @author:邓盈盈
     * @date: 2024/3/8 下午 03:20
     * @param:
     * @return:
     **/
    @GetMapping("/getDetailByName/{service_name}")
    public Result getDetailByName(@PathVariable("service_name")String service_name){
        List<ServiceDetails> result = serviceDetailsService.getDetailByName(service_name);
        if(result!=null){
            return new Result(true,result);
        }else{
            return new Result(false,"fail");
        }

    }
    /**
     * @description:添加新的服务
     * @author:邓盈盈
     * @date: 2024/3/8 下午 06:17
     * @param:
     * @return:
     **/
    @PostMapping("/insertServiceDetail")
    public Result insertServiceDetail(@RequestBody ServiceDetails serviceDetails){
        int result = serviceDetailsService.insertServiceDetail(serviceDetails);
        if(result>0){
            return new Result(true,result);
        }else{
            return new Result(false,"fail");
        }

    }
}
